import { makePicLocal } from "../utils/filesBox.js"
import delay from "../utils/delay.js";
import { searchPicture } from "../api/index.js" 
// import { FileBox } from 'file-box'
import { saveFile, removeFile } from "../utils/fs.js"
const getPicSearch = async (room, content,contact) => {
    try {
        content = content.trim()
        console.log(content.split("#"))
        let key = content.split("#")[1] || content.split('＃')[1]
        if(!key){
            room.say('想要要AI作图，请@我说“AI制图”，并用`#` 拼接关键字，如 “AI制图#日出”')
           return
        }
        key=key.trim()
        console.log(key)
        const res = await searchPicture({ msg: key })
        console.log('搜索图片逻辑信息')
        console.log(res)
        if (res.data) {
            const buffer = Buffer.from(res.data.data[0].b64_image, 'base64')
            const url = saveFile(buffer, res.data.id + '.png')
            const fileBox = makePicLocal(url)
            if(fileBox){
            await room.say(fileBox)
            delay(2000)
            removeFile(url)
            }else{
                room.say `${contact} 图片搜索不到了 去网上看一眼吧……`
            }

        }

    } catch (e) {
        console.log(e)
    }
}
export default getPicSearch